package com.demo.juc.future;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2023/5/24 16:02
 * @Version: 1.0
 */
public class FutureTest {

    public static void main(String[] args) throws InterruptedException, ExecutionException {

        long start = System.currentTimeMillis();

//        Callable<String> cal1 = ()->{
//            try {
//                Thread.sleep(1000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return "凉菜准备完毕";
//        };

        FutureTask<String> task1 = new FutureTask<>(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "凉菜准备完毕";
        });
        new Thread(task1).start();



        Callable<String> cal2 = ()->{
            Thread.sleep(3000); //可处理异常
            return "包子准备完毕";
        };

        FutureTask<String> task2 = new FutureTask<>(cal2);
        new Thread(task2).start();

        System.out.println(task1.get());
        System.out.println(task2.get());

        long end = System.currentTimeMillis();
        System.out.println("准备完毕时间："+(end-start));

    }
}
